Imports System.Drawing.Imaging
Imports System.drawing
Imports System.Drawing.Drawing2D

Namespace FMIS.Portal.Common

    Partial Class FMISThumbnail
        Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

        'This call is required by the Web Form Designer.
        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

        End Sub

        Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
            'CODEGEN: This method call is required by the Web Form Designer
            'Do not modify it using the code editor.
            InitializeComponent()
        End Sub

#End Region

        Function ThumbnailCallback() As Boolean
            Return False
        End Function
        Private Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
            Dim imageURL As String = Request.QueryString("img")
            Dim strWatermark = ""
            If Not Request.QueryString("wtm") Is Nothing Then
                strWatermark = Request.QueryString("wtm")
            End If
            Dim imageWidthQr As Integer = Request.QueryString("width")
            Dim imageHeightQr As Integer = Request.QueryString("height")

            Dim imageWidth As Integer
            Dim imageHeight As Integer



            Dim CurrentimgHeight As Integer
            Dim CurrentimgWidth As Integer


            'Lay dia chi Vat ly cua image dua vao
            imageURL = Server.MapPath(imageURL)

            Dim fullSizeImg As System.Drawing.Image
            Try
                fullSizeImg = System.Drawing.Image.FromFile(imageURL)
            Catch ex As Exception
                fullSizeImg = System.Drawing.Image.FromFile(Server.MapPath("images") & "/noimage.jpg")
            End Try


            CurrentimgHeight = fullSizeImg.Height
            CurrentimgWidth = fullSizeImg.Width

            'Lay kieu anh
            ' ImageType = fullSizeImg.ci

            'Lay kich thuoc co the tao Thumbnail chuan theo Height hoac Width dua vao  
            'Theo Height

            ' kiem tra xem co phai la co mot chieu khong
            If imageHeightQr = 0 Or imageWidthQr = 0 Then
                ' neu co mot chieu theo chieu dai
                If imageHeightQr = 0 Then
                    ' lay gia tri chieu dai limit
                    Dim limitHeight As Integer = IIf(Request.QueryString("limitheight") Is Nothing, 0, Request.QueryString("limitheight"))

                    imageWidth = imageWidthQr
                    imageHeight = CurrentimgHeight / CurrentimgWidth * imageWidthQr

                    ' kiem tra xem sau khi co chieu dai co vuot qua gioi han cho phep khong
                    If imageHeight > limitHeight And limitHeight <> 0 Then
                        ' neu vuot qua thi xac dinh lai hai chieu de tien hanh co hai chieu
                        imageHeightQr = limitHeight

                        If (CurrentimgHeight / CurrentimgWidth * imageWidthQr) >= imageHeightQr Then
                            imageHeight = imageHeightQr
                            imageWidth = CurrentimgWidth / CurrentimgHeight * imageHeightQr
                            'Theo Width
                        Else
                            imageWidth = imageWidthQr
                            imageHeight = CurrentimgHeight / CurrentimgWidth * imageWidthQr
                        End If

                    End If
                End If
                ' neu la co theo chieu rong
                If imageWidthQr = 0 Then

                    Dim limitWidth As Integer = IIf(Request.QueryString("limitwidth") Is Nothing, 0, Request.QueryString("limitwidth"))

                    imageHeight = imageHeightQr
                    imageWidth = CurrentimgWidth / CurrentimgHeight * imageHeightQr

                    If imageWidth > limitWidth And limitWidth <> 0 Then
                        imageWidthQr = limitWidth

                        If (CurrentimgHeight / CurrentimgWidth * imageWidthQr) >= imageHeightQr Then
                            imageHeight = imageHeightQr
                            imageWidth = CurrentimgWidth / CurrentimgHeight * imageHeightQr
                            'Theo Width
                        Else
                            imageWidth = imageWidthQr
                            imageHeight = CurrentimgHeight / CurrentimgWidth * imageWidthQr
                        End If

                    End If

                End If

            Else
                If (CurrentimgHeight / CurrentimgWidth * imageWidthQr) >= imageHeightQr Then
                    imageHeight = imageHeightQr
                    imageWidth = CurrentimgWidth / CurrentimgHeight * imageHeightQr
                    'Theo Width
                Else
                    imageWidth = imageWidthQr
                    imageHeight = CurrentimgHeight / CurrentimgWidth * imageWidthQr
                End If

            End If

            'Neu anh co kich thuoc nho hon chieu cao va chieu rong input
            'Thi giu nguyen kich thuoc anh
            If (CurrentimgHeight <= imageHeightQr And CurrentimgWidth <= imageWidthQr) Then
                imageWidth = CurrentimgWidth
                imageHeight = CurrentimgHeight
            End If

            'Chi lam viec voi anh JPG

            Response.ContentType = "image/jpeg"
            If imageHeight > 0 And imageWidth > 0 Then
                Dim dummyCallBack As System.Drawing.Image.GetThumbnailImageAbort
                dummyCallBack = New System.Drawing.Image.GetThumbnailImageAbort(AddressOf ThumbnailCallback)


                Dim thumbNailImg As System.Drawing.Image
                thumbNailImg = fullSizeImg.GetThumbnailImage(imageWidth, imageHeight, dummyCallBack, IntPtr.Zero)


                Dim g As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(thumbNailImg)

                'Sets the interpolation mode for a high quality image    		
                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic
                g.DrawImage(fullSizeImg, 0, 0, imageWidth, imageHeight)
                g.SmoothingMode = SmoothingMode.HighQuality

                'Neu co WTM
                If Not Request.Params("wtm") Is Nothing Then
                    Dim StringSizeF As SizeF, DesiredWidth As Single, wmFont As Font, RequiredFontSize As Single, Ratio As Single
                    'Set font cho text watermark	 

                    wmFont = New Font("Verdana", 14, FontStyle.Bold)
                    DesiredWidth = imageWidth * 0.5

                    'Can vi tri wtm len giua anh    	
                    StringSizeF = g.MeasureString(strWatermark, wmFont)
                    Ratio = StringSizeF.Width / wmFont.SizeInPoints
                    RequiredFontSize = DesiredWidth / Ratio
                    wmFont = New Font("Verdana", RequiredFontSize, FontStyle.Bold)

                    Dim letterBrush As SolidBrush = New SolidBrush(Color.FromArgb(50, 255, 255, 255))
                    Dim shadowBrush As SolidBrush = New SolidBrush(Color.FromArgb(50, 0, 0, 0))

                    'Nhap xau wtm 
                    g.DrawString(strWatermark, wmFont, shadowBrush, 75, (imageHeight * 0.5) - 36)
                    g.DrawString(strWatermark, wmFont, letterBrush, 77, (imageHeight * 0.5) - 38)

                End If

                thumbNailImg.Save(Response.OutputStream, ImageFormat.Jpeg)


            Else
                fullSizeImg.Save(Response.OutputStream, ImageFormat.Jpeg)

            End If
            'Lock viec thay doi anh        
            fullSizeImg.Dispose()

        End Sub
    End Class
End Namespace